<template>
  <div class="event-info">
    <el-form :model="ruleForm" :rules="rules" ref="UserForm" label-width="120px" label-suffix="：">
      <div class="event-content">
        <el-form-item label="合作伙伴名称" prop="partnerName">
          <div class="cont-input">
            <el-input v-model="ruleForm.partnerName" placeholder="请输入合作伙伴名称"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="合作伙伴全称" prop="partnerFullName">
          <div class="cont-input">
            <el-input v-model="ruleForm.partnerFullName" placeholder="请输入合作伙伴全称"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="合作伙伴图片" prop="thumbSrc">
          <UpLoad 
            v-model="ruleForm.thumbSrc" 
            :limit="thumbSrcProp.limit"
            :editId="editId"
            :type="thumbSrcProp.type"
            :filelist="filelist"
            @geturl="getPictureUrl"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sortId">
          <div class="cont-input">
            <el-input v-model="ruleForm.sortId" placeholder="请输入排序"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <div class="cont-input">
            <el-input v-model="ruleForm.remark" type="textarea" placeholder="请输入备注"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="isActivity">
          <div class="cont-input">
            <el-switch
              v-model="ruleForm.isActivity"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </div>
        </el-form-item>
      </div>
      <div class="cont-footer">
        <el-button @click.native="cancelPop">取消</el-button>
        <template v-if="commonTabInfo.text === '新增'">
          <el-button type="primary" @click.native="addPartnerEvent">保存</el-button>
        </template>
        <template v-else-if="commonTabInfo.text === '编辑'">
          <el-button type="primary" @click.native="editPartnerEvent">保存编辑</el-button>
        </template>
      </div>
    </el-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { AddOrUpdatePartner, GetPartnerSingle, DeletePic } from '@/api/requestConfig'
  import UpLoad from '@/components/Upload'
  export default {
    name: 'UserInfo',
    data() {
      return {
        ruleForm: {
          isActivity: 1,
          partnerName: '',
          partnerFullName: '',
          sortId: '',
          remark: '',
          thumbSrc: ''
        },
        editId: 0, // 编辑Id
        filelist: [], // 编辑图片
        thumbSrcProp: {
          type: 2, // 消息发布1；合作伙伴2；广告3；
          limit: 1
        }, // 图片上传组件参数
        rules: {
          partnerName: [
            { required: true, message: '请输入合作伙伴名称', trigger: 'blur' }
          ],
          thumbSrc: [
            { required: true, message: '请上传合作伙伴logo', trigger: 'change' }
          ]
        },
        editData: {},
        addFlag: true
      }
    },
    computed: {
      ...mapGetters([
        'commonTabInfo'
      ])
    },
    components: {
      UpLoad
    },
    created() {
      // 编辑时获取数据
      if (this.commonTabInfo.text === '编辑') {
        this.editId = this.commonTabInfo.custom.id
        GetPartnerSingle(this.commonTabInfo.custom.id).then(res => {
          if (res.data) {
            const resData = res.data
            this.editData = resData
            if (this.editData) {
              for (var key in this.ruleForm) {
                this.ruleForm[key] = this.editData[key]
              }
              // 渲染图片
              if (this.ruleForm.thumbSrc) {
                var resultList = []
                var srcArr = this.ruleForm.thumbSrc.split(',')
                for (var i = 0; i < srcArr.length; i++) {
                  var obj = {}
                  obj.name = srcArr[i]
                  obj.url = srcArr[i]
                  resultList.push(obj)
                }
                this.filelist = resultList
                this.ruleForm.thumbSrc = resultList
              }
              console.log(this.filelist)
            }
          }
        })
      }
    },
    methods: {
      // tabs操作
      eventTab(text, tabsName, custom) {
        this.$store.dispatch('handleTabInfo', {
          text: text,
          tabsName: tabsName,
          custom: custom
        })
      },
      // 取消
      cancelPop() {
        // 新增时如果上传了图片，需删除
        var self = this
        if (this.commonTabInfo.text === '新增') {
          var picUrl = ''
          if (self.ruleForm.thumbSrc.length > 0) {
            for (var i = 0; i < self.ruleForm.thumbSrc.length; i++) {
              if (i === 0) {
                picUrl = picUrl + self.ruleForm.thumbSrc[i].response.data
              } else {
                picUrl = picUrl + ',' + self.ruleForm.thumbSrc[i].response.data
              }
            }
          }
          var needData = {}
          needData.picName = picUrl
          needData.type = this.thumbSrcProp.type
          DeletePic(needData)
        }
        this.eventTab('', 'first')
      },
      // 获取图片上传路径
      getPictureUrl(fileList) {
        this.ruleForm.thumbSrc = fileList
      },
      // 新增
      addPartnerEvent() {
        var self = this
        if (!this.addFlag) {
          return
        }
        this.$refs.UserForm.validate(function(valid) {
          if (valid) {
            var needData = {}
            needData.partnerName = self.ruleForm.partnerName
            needData.partnerFullName = self.ruleForm.partnerFullName
            needData.sortId = self.ruleForm.sortId
            needData.remark = self.ruleForm.remark
            needData.isActivity = self.ruleForm.isActivity
            var picUrl = ''
            if (self.ruleForm.thumbSrc.length > 0) {
              for (var i = 0; i < self.ruleForm.thumbSrc.length; i++) {
                if (i === 0) {
                  picUrl = picUrl + self.ruleForm.thumbSrc[i].response.data
                } else {
                  picUrl = picUrl + ',' + self.ruleForm.thumbSrc[i].response.data
                }
              }
            }
            needData.thumbSrc = picUrl
            self.addFlag = false
            AddOrUpdatePartner(needData).then(res => {
              if (res.code === 200) {
                self.$message.success(res.message)
                setTimeout(function() {
                  self.eventTab('refresh', 'first', 'partner')
                }, 500)
              } else {
                self.$message.error(res.message)
              }
            }).catch(error => {
              console.log(error)
            })
          } else {
            return
          }
        })
        setTimeout(function() {
          self.addFlag = true
        }, 3000)
      },
      // 编辑
      editPartnerEvent() {
        var self = this
        if (!this.addFlag) {
          return
        }
        this.$refs.UserForm.validate(function(valid) {
          if (valid) {
            var needData = {}
            needData.id = self.editData.id
            needData.partnerName = self.ruleForm.partnerName
            needData.partnerFullName = self.ruleForm.partnerFullName
            needData.sortId = self.ruleForm.sortId
            needData.remark = self.ruleForm.remark
            needData.isActivity = self.ruleForm.isActivity
            var picUrl = ''
            if (self.ruleForm.thumbSrc.length > 0) {
              for (var i = 0; i < self.ruleForm.thumbSrc.length; i++) {
                var thisUrl = ''
                if (self.ruleForm.thumbSrc[i].response && self.ruleForm.thumbSrc[i].response.data) {
                  thisUrl = self.ruleForm.thumbSrc[i].response.data
                } else {
                  thisUrl = self.ruleForm.thumbSrc[i].url
                }
                if (i === 0) {
                  picUrl = picUrl + thisUrl
                } else {
                  picUrl = picUrl + ',' + thisUrl
                }
              }
            }
            needData.thumbSrc = picUrl
            self.addFlag = false
            AddOrUpdatePartner(needData).then(res => {
              if (res.code === 200) {
                self.$message.success(res.message)
                setTimeout(function() {
                  self.eventTab('update', 'first', 'partner')
                }, 500)
              } else {
                self.$message.error(res.message)
              }
            }).catch(error => {
              console.log(error)
            })
          } else {
            return
          }
        })
        setTimeout(function() {
          self.addFlag = true
        }, 3000)
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/info.scss";
</style>
